<?php
/**
* 关于数据库查询的函数库
* $Id: function_query.php 168 2010-05-13 10:06:14Z bwanglei $
*/

/**
* 取得所有分区和版面信息并返回为一个数组
*/
function apf_banmian_query($mysql_conn) {
	// 取得分区信息和版面信息返回到一个数组中
	$query_banmian = "select * from `".DB_PREFIX."banmian`";
	$result_banmian = $mysql_conn->query($query_banmian);
	$count_banmian = $result_banmian->num_rows;
	for ($i=0; $i<$count_banmian; $i++) {
		// 通过 while 循环把分区下的所有版面信息加入数组
		$array_banmian[$i] = $result_banmian->fetch_assoc();
		$current_banmian_id = $array_banmian[$i]['id'];
		$query_banmian_2 = "select * from `".DB_PREFIX."banmian_2` where `father_id`=".$current_banmian_id;
		$result_banmian_2 = $mysql_conn->query($query_banmian_2);
		while ($row_banmian_2 = $result_banmian_2->fetch_assoc()) {
			// 如果存在最新帖子，则加入数组
			$banmian_2_id = $row_banmian_2['id'];
			$query_banzhu = 'select `username` from `apf_banzhu_banmian` where `banmian_id`='.$banmian_2_id;
			$result_banzhu = $mysql_conn->query($query_banzhu);
			if (!empty($result_banzhu) && $result_banzhu->num_rows>0) {
				$array_banzhu = array();
				while ($row_result_banzhu = $result_banzhu->fetch_assoc()) {
					$array_banzhu[] = $row_result_banzhu['username'];
				}
				$row_banmian_2['banzhu'] = $array_banzhu;
			}
			$query_rec_thread = "select * from `".DB_PREFIX."thread` where `banmian_id`=".$banmian_2_id." order by `id` desc limit 1";
			$result_rec_thread = $mysql_conn->query($query_rec_thread);
			if ($result_rec_thread!=Null and $result_rec_thread->num_rows>0) {
				$row_banmian_2['rec_thread'] = $result_rec_thread->fetch_assoc();
			}
			$array_banmian[$i]['banmian_2'][] = $row_banmian_2;
		}
	}
	return $array_banmian;
}

/**
* 取得某个表的记录总数
*/
function apf_table_count($mysql_conn,$table) {
	$query = "select count(*) as `count` from `".$table."`";
	$result = $mysql_conn->query($query);
	$row_count = $result->fetch_assoc();
	$count = $row_count['count'];
	return $count;
}

/**
* 获得版面下的主题总数
*/
function apf_get_count_thread_banmian($mysql_conn,$banmian_id) {
	$query = "select count(*) as `count` from ".DB_PREFIX."thread where banmian_id=".$banmian_id;
	$result = $mysql_conn->query($query);
	$result = $result->fetch_assoc();
	return $result['count'];
}

/**
* 取得版面的版面分页总数
*/
function apf_get_count_pages_banmian($mysql_conn,$id_banmian,$apf_settings) {
	$count_threads_banmian = $apf_settings['count_threads_banmian'];
	$count_threads = apf_get_count_thread_banmian($mysql_conn, $id_banmian);
	$page_count_banmian = $count_threads/$count_threads_banmian;
	if ($page_count_banmian<=0) {
		$page_count_banmian = 0;
	} else {
		$page_count_banmian = ceil($page_count_banmian);
	}
	return $page_count_banmian;
}

/**
* 取得某个版面的主题信息
*/
function apf_articles_banmian($mysql_conn,$banmian_id,$num_page,$apf_settings,$type='all') {
	$page_count_banmian = apf_get_count_pages_banmian($mysql_conn,$banmian_id,$apf_settings);
	if ($num_page > $page_count_banmian-1) {
		$num_page = $page_count_banmian-1;
	} elseif ($num_page < 0) {
		$num_page = 0;
	}

	$query_addstr = '';
	if ($type=='start') $query_addstr .= ' and `is_jing`=1';
	$query_threads = "select ".DB_PREFIX."thread.*,
		".DB_PREFIX."article_b.username,
		".DB_PREFIX."article_b.content,
		".DB_PREFIX."article_b.time_date as article_time_date
		from `".DB_PREFIX."thread` left join 
		(select * from ".DB_PREFIX."article order by time_date desc) as apf_article_b
		on ".DB_PREFIX."thread.id = ".DB_PREFIX."article_b.thread_id
		where ".DB_PREFIX."thread.banmian_id=".$banmian_id.$query_addstr."
		group by ".DB_PREFIX."thread.id
		order by apf_thread.is_top desc, ".DB_PREFIX."article_b.time_date desc
		limit ".$num_page*$apf_settings['count_threads_banmian'].",".$apf_settings['count_threads_banmian'];
	$result_threads = $mysql_conn->query($query_threads);
	// 加入评论个数
	$array_threads = array();
	if (!empty($result_threads) && $result_threads->num_rows>0) {
	while ($thread_row = $result_threads->fetch_assoc()) {
		$count_articles = apf_count_articles_thread($thread_row['id'],$mysql_conn);
		$thread_row['article_count'] = $count_articles;
		$array_threads[] = $thread_row;
	}}
	return $array_threads;
}

/**
* 取得某个版面的主题线索和全部帖子信息
*/
function apf_get_article_thread_banmian($banmian_id,$num_page) {
	global $mysql_conn;
	global $apf_settings;
	$threads = apf_articles_banmian($mysql_conn,$banmian_id,$num_page,$apf_settings);
	foreach ($threads as $key=>$thread) {
		$thread_id = $thread['id'];
		$query = 'select * from `apf_article` where `thread_id`='.$thread_id.' order by time_date desc limit 1,100';
		$result = $mysql_conn->query($query);
		$articles_thread = array();
		if (!empty($result) && $result->num_rows>0) {
			while ($article_thread=$result->fetch_assoc()) {
				$articles_thread[] = $article_thread;
			}
			$threads[$key]['articles']=$articles_thread;
		}
	}
	return $threads;
}

/**
* 取得主题下帖子个数
*/
function apf_count_articles_thread($thread_id,$mysql_conn) {
	$query = "select count(*) as `count` from ".DB_PREFIX."article where `thread_id`=".$thread_id;
	$result = $mysql_conn->query($query);
	if (isset($result) and ($result!=Null)) {
		$result = $result->fetch_assoc();
		$count = $result['count'];
	} else {
		$count = 0;
	}
	return $count;
}

/**
* 取得主题下的分页数
*/
function apf_count_page_thread($per_page_count,$thread_id,$mysql_conn) {
	$count_articles_thread = apf_count_articles_thread($thread_id,$mysql_conn);
	$page_count = intval(ceil($count_articles_thread/$per_page_count));
	return $page_count;
}

/**
* 取得一个主题下的帖子
*/
function apf_get_thread($mysql_conn,$thread_id,$num_page,$apf_settings) {
	$query = "select ".DB_PREFIX."thread.*,
		".DB_PREFIX."article.id as article_id,
		".DB_PREFIX."article.username,
		".DB_PREFIX."article.content,
		".DB_PREFIX."article.time_date
		from ".DB_PREFIX."thread left join ".DB_PREFIX."article
		on ".DB_PREFIX."thread.id = ".DB_PREFIX."article.thread_id
		where ".DB_PREFIX."thread.id = ".$thread_id."
		order by ".DB_PREFIX."article.time_date asc
		limit ".($num_page-1)*$apf_settings['count_articles_thread'].",".$apf_settings['count_articles_thread'];
	$result = $mysql_conn->query($query);
	$articles_thread = array();
	while ($result_row = $result->fetch_assoc()) {
		$articles_thread[] = $result_row;
	}
	return $articles_thread;
}

/**
* 取得某个用户发送帖子所组成的总页数
*/
function apf_user_article_page_count($username,$per_page=15) {
	if ($per_page == 0) return false;
	global $mysql_conn;
	$query = 'select count(*) as `count` from `apf_article` where `username`="'.$username.'"';
	$result = $mysql_conn->query($query);
	if ($result->num_rows>0) {
		$result = $result->fetch_assoc();
		$count = $result['count'];
		$count = intval(ceil($count/$per_page));
		return $count;
	}
	return 0;
}

/**
* 取得某用户所参与的帖子
*/
function apf_get_user_articles($username,$page_count,$per_page=15,$page_num=0) {
	global $mysql_conn;
	$query = 'select a.id, a.username, a.thread_id, a.time_date, t.banmian_id, t.count_read, t.title
		from `apf_article` as a left join `apf_thread` as t
		on a.thread_id=t.id
		where a.username="'.$username.'"
		order by a.time_date desc
		limit '.$per_page*$page_num.','.$per_page;
	$result = $mysql_conn->query($query);
	$r_result = array();
	while ($row = $result->fetch_assoc()) {
		$r_result[] = $row;
	}
	return $r_result;
}

/**
* 根据 article id 删除帖子
*/
function apf_del_article($aid) {
	global $mysql_conn;
	$query = 'delete from `apf_article` where `id`='.$aid;
	$mysql_conn->query($query);
	if ($mysql_conn->affected_rows==1) return true;
	return false;
}

/**
* 删除主题和主题下的所有帖子
*/
function apf_del_thread($tid) {
	global $mysql_conn;
	$query = 'delete from `apf_thread` where `id`='.$tid;
	$mysql_conn->query($query);
	if ($mysql_conn->affected_rows==1) {
		$query = 'delete from `apf_article` where `thread_id`='.$tid;
		$mysql_conn->query($query);
		if ($mysql_conn->affected_rows>0) return $mysql_conn->affected_rows;
	}
	return false;
}

/**
* 获取某个帖子的信息
*/
function apf_get_article($aid) {
	global $mysql_conn;
	$query = 'select * from `apf_article` where `id`='.$aid;
	$result = $mysql_conn->query($query);
	if ($result->num_rows==1) {
		$article=$result->fetch_assoc();
		$t_id = $article['thread_id'];
		$query = 'select `title` from `apf_thread` where `id`='.$t_id;
		$result = $mysql_conn->query($query);
		if ($result->num_rows==1) {
			$result = $result->fetch_assoc();
			$article['title'] = $result['title'];
			return $article;
		}
	}
	return false;
}

/**
* 取得当前主题阅读次数
*/
function apf_get_read_count_thread($tid) {
	global $mysql_conn;
	$query = 'select `count_read` from `apf_thread` where `id`='.$tid;
	$result = $mysql_conn->query($query);
	if (!empty($result) && $result->num_rows==1) {
		$result = $result->fetch_assoc();
		$result = $result['count_read'];
		return $result;
	}
	return false;
}

/**
* 帖子阅读次数加1
*/
function apf_add_read_count_thread($tid,$count) {
	global $mysql_conn;
	$query = 'update `apf_thread` set `count_read`='.($count+1).' where `id`='.$tid;
	$mysql_conn->query($query);
	if ($mysql_conn->affected_rows==1) return true;
	return false;
}

/**
* 添加新主题
*/
function apf_add_thread($title,$content,$banmian_id,$username) {
	global $mysql_conn;
	$query = 'insert into `apf_thread` (banmian_id,title) values ('.$banmian_id.',"'.$title.'")';
	$mysql_conn->query($query);
	if ($mysql_conn->affected_rows==1) {
		$query = 'select last_insert_id()';
		$result = $mysql_conn->query($query);
		$tid = $result->fetch_array();
		$tid = $tid[0];
		if (isset($tid)) {
			$query = 'insert into `apf_article` (`username`,`content`,`thread_id`,`time_date`)
				values ("'.$username.'","'.$content.'",'.$tid.','.time().')';
			$mysql_conn->query($query);
			if ($mysql_conn->affected_rows==1) return true;
		}
	}
	return false;
}

/**
* 列出版面
*/
function apf_get_banmians($banmian='all') {
	global $mysql_conn;
	// 取得所有分区信息
	$query = 'select * from apf_banmian';
	$result = $mysql_conn->query($query);
	if (!empty($result) && $result->num_rows>0) {
		$fenqus = array();
		while ($fenqu = $result->fetch_assoc()) {
			$fenqus[]=$fenqu;
		}
	}

	// 取得版面信息
	if ($banmian=='all') {
		$query = 'select * from `apf_banmian_2`';
	} else {
		if (count($banmian)==1) {
			$query = 'select * from `apf_banmian_2` where `id`='.$banmian[0];
		} else {
			foreach ($banmian as $key=>$value) {
				if ($key==0) {
					$str_banmian=$value;
				} else {
					$str_banmian .= (','.$value);
				}
			}
			$query = 'select * from `apf_banmian_2` where `id` in '.$str_banmian;
		}
	}
	$result = $mysql_conn->query($query);
	if (!empty($result) && $result->num_rows>0) {
		$r_banmian = array();
		while ($row = $result->fetch_assoc()) {
			$r_banmian[] = $row;
		}
	}

	$array_banmian = array();
	foreach ($fenqus as $fenqu) {
		foreach ($r_banmian as $b) {
			if ($fenqu['id']==$b['father_id']) {
				$fenqu['banmians'][]=$b;
				$array_banmian[] = $fenqu;
			}
		}
	}
	return $array_banmian;
}

/**
* 取得后台修改版面的页面数
*/
function apf_edit_banmian_count($banmian_id,$per_count=15) {
	global $mysql_conn;
	$count_thread = apf_get_count_thread_banmian($mysql_conn,$banmian_id);
	$count_page = intval(ceil($count_thread/$per_count));
	return $count_page;
}

//function apf_articles_banmian($mysql_conn,$banmian_id,$num_page,$apf_settings) {

/**
* 取得某个版面的主题和帖子信息
*/
function get_thread_article_onbanmian($banmian_id,$per_count,$num_page) {
	global $mysql_conn;
	global $apf_settings;
	$query = 'select * from `apf_thread` order by `time_date` dese limit '.$per_count*$num_page.','.$per_count;
	$result = $mysql_conn->query($query);
	if (!empty($result) && $result->num_rows>0) {
		$thread_a = array();
		while ($row = $result->fetch_assoc()) {
			$thread_a[] = $row;
		}
		return $thread_a;
	}
	return false;
}

/**
* 截取字符串函数 固定长度字符串的截取
*/
function apf_toFixLen($str,$len) {
	if($len>=strlen($str)||!$len) return $str;
	$len-=3;
	$tempstr1 = substr($str,0,$len);	//截取字符串
	$tempstr2 = preg_replace('/([\x81-\xff]+)$/ms','',$tempstr1,1);	//去掉结尾的连续汉字字符
	if(!is_int((strlen($tempstr1)-strlen($tempstr2))/2)) {
		//去掉的字符为奇数?
		$tempstr1 = substr($str,0,$len-1);
	}
	return $tempstr1."…";
}
?>
